package com.ssy.lingxi.member.merchant.handler.validator;

import com.ssy.lingxi.common.utils.IDCardNumberUtil;
import com.ssy.lingxi.member.merchant.handler.annotation.IDCardNumberAnnotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 身份证号码注解校验实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-06-19
 */
public class IDCardNumberValidator implements ConstraintValidator<IDCardNumberAnnotation, String> {
    private boolean required;

    @Override
    public void initialize(IDCardNumberAnnotation constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(!required) {
            return true;
        }
        return IDCardNumberUtil.isIDNumber(value);
    }
}
